//
//  UIButton+HitEdge.swift
//  CCFoundation_Swift
//
//  Created by zjh on 2022/2/12.
//

import UIKit
private var hitEdgeKey: UInt8 = 0

extension UIButton {
    
    /// 扩展的边距  UIEdgeInsets(top: -20, left: -20, bottom: -20, right: -20)
    /// 小于0时为扩大触发范围，大于0触发范围缩小
    var hitEdge: UIEdgeInsets {
        set {
            objc_setAssociatedObject(self, &hitEdgeKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
        
        get {
            guard let tempHitEdge = objc_getAssociatedObject(self, &hitEdgeKey) as? UIEdgeInsets else {
                return .zero
            }
            return tempHitEdge
        }
    }
    
    /// 重写point inside方法
    /// - Parameters:
    ///   - point: <#point description#>
    ///   - event: <#event description#>
    /// - Returns: <#description#>
    override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if (self.hitEdge == .zero) {
            return super.point(inside: point, with: event)
        }
        let hitFrame = self.bounds.inset(by: self.hitEdge)
        return hitFrame.contains(point)
    }
}
